<?php

namespace app\api\controller;
/**
 * ============================================================================
 * DSShop单店铺商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 浏览历史控制器
 */
class Membergoodsbrowse extends MobileMember {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 我的足迹
     */
    public function browse_list() {
        $goodsbrowse_model = model('goodsbrowse');
    	$page = intval(input('param.page'));
    	$per_page = intval(input('param.per_page'));
        //查询浏览记录
        $condition = array();
        $condition[] = array('member_id','=',$this->member_info['member_id']);
    	$timearray = $goodsbrowse_model->getGoodsbrowseList($condition,'goodsbrowse_time',0,'goodsbrowse_time desc');
    	foreach($timearray as $key => $val){
    		$timearray[$key]['goodsbrowse_time'] = date('Y-m-d',$val['goodsbrowse_time']);
    	}
    	
    	$timearray = array_unique(array_column($timearray,'goodsbrowse_time'));//去除日期数组里面的重复日期
    	$count = count($timearray);//日期数组长度
    	$page_total = ceil($count/$per_page);//总页数
    	if(($count - ($per_page * $page)) > 0){
    		$hasmore = true;
    	}else{
    		$hasmore = false;
    	}
    	
    	$start = $per_page * ($page - 1);//数组起始位置
    	$pagetimearray = array_slice($timearray,$start,$per_page);//获取页数日期数组
    	$browsegoodslist = array();
    	foreach($pagetimearray as $key => $val){
    		$browsegoodslist[$key]['time'] =$val;
    		$condition   = array();
    		$condition[] = array('member_id' ,'=', $this->member_info['member_id']);
    		$condition[] = array('goodsbrowse_time' ,'>=', strtotime($val));
    		$condition[] = array('goodsbrowse_time' ,'<=', strtotime($val)+86399);
    		$goodsidarray = $goodsbrowse_model->getGoodsbrowseList($condition,'goods_id');
    		$goodsidarray = array_column($goodsidarray,'goods_id');
    		$goodsidarray = implode(',',$goodsidarray);
    		$goods_list = model('goods')->getGoodsList(array(array('goods_id','in', $goodsidarray)), 'goods_id, goods_name, goods_advword, goods_promotion_price, goods_promotion_type, goods_image, gc_id, gc_id_1, gc_id_2, gc_id_3');
    		foreach($goods_list as $k => $v){
    			$goods_list[$k]['goods_image_url'] = goods_cthumb($v['goods_image'], 480);
    		}
    		$browsegoodslist[$key]['goods_list'] =$goods_list;
    	}
        $result = array_merge(array('goodsbrowse_list' => $browsegoodslist),array('hasmore' => $hasmore),array('page_total' => $page_total));
        ds_json_encode(10000, '', $result);
    }

    /**
     * 清空足迹
     */
    public function browse_clearall() {
        //清除缓存中浏览记录
        dcache($this->member_info['member_id'], 'goodsbrowse');
        model('goodsbrowse')->delGoodsbrowse(array('member_id' => $this->member_info['member_id']));
        ds_json_encode(10000, '',1);
    }

}